<?php
	/** 发送消息类
	@author ffpan
	@date 2010-04-10
	@version 1.0
	**/

	class Message{
		function __construct(){
			$this->fangDb = new wbDatabase();
		}

		function __destruct(){}

		function __get($att){
			return $att;
		}

		function __set($att , $val){
			$this->$att = $val;
		}

		function safeVal($val){
			if (!get_magic_quotes_gpc()) {
				 $val = addslashes($val);
			}
			return $val;
		}

		function safePostVal(){
			if (!get_magic_quotes_gpc()) {
			   foreach($_POST as &$v){
				 $v = addslashes($v);
			   }
			}
			return $val;
		}
		
		
		
		//发送一条消息
		function addOneMessage(){
			$this->safePostVal();
			$title = $_REQUEST["title"];
			$content = $_REQUEST["content"];
			$sender = __user_prop__==0?"a_".__user_id__:"u_".__user_id__;
			$receiver = $_REQUEST["receiver"];
			

			$sql = "insert into ".__table_message__." 
					values(
					'',
					'".$sender."',
					'".$receiver."',
					'".$title."',
					'".$content."',
					now() , 
					0
					)";
			$result = $this->fangDb->query($sql);
			if($result){
				return "success";
			}else{
				return "fail";
			}
		}

		//获取信息列表
		function getMessageList(){
			if(__user_prop__==0){
				$receiver = "a_".__user_id__;
			}else if(__user_prop__==1){
				$receiver = "u_".__user_id__;
			}else if(__user_prop__==3){
				$receiver = "fwdk_".__user_id__;
			}
			//$receiver = __user_prop__==0?"a_".__user_id__:"u_".__user_id__;
			$sql = "select * from ".__table_message__." where receiver='".$receiver."' order by senddate desc";
			$result = $this->fangDb->query($sql);
			$arr = array();
			if($result){
				while($row = $result->fetch_assoc()){
					$temp = $this->getNamesById($row["sender"]);
					$row["sendername"] =  $temp["sendername"];
					$row["senderlink"] = $temp["senderlink"];
					$arr[] = $row;
				}
			}
			return $arr;
		}

		function getMsgNum(){
			if(__user_prop__==0){
				$receiver = "a_".__user_id__;
			}else if(__user_prop__==1){
				$receiver = "u_".__user_id__;
			}else if(__user_prop__==3){
				$receiver = "fwdk_".__user_id__;
			}
			$sql = "select count(*) from ".__table_message__." where receiver='".$receiver."' and readflag=0";
			$result = $this->fangDb->query($sql);
			$row = $result->fetch_array();
			return $row[0];
		}
	
		function getKuserMessageList(){
			$receiver = "fwdk_".__user_id__;
			$sql = "select * from ".__table_message__." where receiver='".$receiver."' order by senddate desc";
			$result = $this->fangDb->query($sql);
			$arr = array();
			if($result){
				while($row = $result->fetch_assoc()){
					$temp = $this->getNamesById($row["sender"]);
					$row["sendername"] =  $temp["sendername"];
					$row["senderlink"] = $temp["senderlink"];
					$arr[] = $row;
				}
			}
			return $arr;
		}

	
		//获取一条信息
		function getMessageInfo($id){
			$id = $this->safeVal($id);
			$sql = "update ".__table_message__." set readflag = 1 where id=".$id." limit 1";
			$result = $this->fangDb->query($sql);
			$sql = "select * from ".__table_message__." where id = '".$id."' limit 1";
			$result = $this->fangDb->query($sql);
			$row = $result->fetch_assoc();
			$result->close();
			$temp = $this->getNamesById($row["sender"]);
			$row["sendername"] = $temp["sendername"];
			$row["senderlink"] = $temp["senderlink"];
			return $row;
		}

		function readOneMessage($id){
			$id = $this->safeVal($id);
			$sql = "update ".__table_message__." set readflag = 1 where id=".$id." limit 1";
			$result = $this->fangDb->query($sql);
			if($result){
				return "success";
			}else{
				return "fail";
			}
		}

		//获取发送者姓名
		function getNamesById($sender){
			$row = array();
			$pp = explode("_" , $sender);
			
			if($pp[0]=="a"){
				$aid = str_replace("a_" , "" , $sender);
				$sql2 = "select id , name from ".__table_agentlist__." where id=".$aid." limit 1";
				$result2 = $this->fangDb->query($sql2);
				if($result2){
					$row2 = $result2->fetch_assoc();
					$row["sendername"] = $row2["name"];
					$row["senderlink"]  = "agent_detail.php?aid=".$row2["id"];
				}
			}else if($pp[0]=="u"){
				$uid = str_replace("u_" , "" , $sender);
				$sql2 = "select id , name from ".__table_userlist__." where id=".$uid." limit 1";
				$result2 = $this->fangDb->query($sql2);
				if($result2){
					$row2 = $result2->fetch_assoc();
					$row["sendername"] = $row2["name"];
					$row["senderlink"]  = "";
				}
			}else if($pp[0]=="fwdk"){
				$kid = str_replace("fwdk_" , "" , $sender);
				$sql2 = "select id , name from ".__table_fwdklist__." where id=".$kid." limit 1";
				$result2 = $this->fangDb->query($sql2);
				if($result2){
					$row2 = $result2->fetch_assoc();
					$row["sendername"] = $row2["name"];
					$row["senderlink"]  = "kuser_detail.php?kid=".$row2["id"]."&lx=2";
				}
			}
			/*if(strpos($sender , "u_") === false){ //发送者为中介
				$aid = str_replace("a_" , "" , $sender);
				$sql2 = "select id , name from ".__table_agentlist__." where id=".$aid." limit 1";
				$result2 = $this->fangDb->query($sql2);
				$row2 = $result2->fetch_assoc();
				$row["sendername"] = $row2["name"];
				$row["senderlink"]  = "agent_detail.php?aid=".$row2["id"];
			}else{
				$uid = str_replace("u_" , "" , $sender);
				$sql2 = "select id , name from ".__table_userlist__." where id=".$uid." limit 1";
				$result2 = $this->fangDb->query($sql2);
				$row2 = $result2->fetch_assoc();
				$row["sendername"] = $row2["name"];
				$row["senderlink"]  = "";
			}*/
			return $row;
		}

			
		//删除一条消息
		function deleteOneMessage(){
			$mid = $this->safeVal($_GET["mid"]);
			$sql = "delete from ".__table_message__." where id=".$mid;
			$result = $this->fangDb->query($sql);
			if($result){
				return "success";
			}else{
				return "fail";
			}
		}

		
	}

	$myMessage = new Message();
?>